//
//  MainTabBar.swift
//  微博个人swift
//
//  Created by 李灏 on 16/1/30.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(plusBtn)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //布局子视图的方法
    override func layoutSubviews() {
        super.layoutSubviews()
        //确定tablebar的大小
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个能够自增的索引
        var index:CGFloat = 0
        
        //遍历子视图找UITabBarButton
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                print(subview)
                //修改frame
                subview.frame = CGRectOffset(rect, w * index, 0)
//                if index == 1{
//                    //多递增一次
//                    index++
//                }
//                index++
                //三目运算符简写
                index += (index == 1 ? 2:1)
            }
        }
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    lazy var plusBtn:UIButton = UIButton(backImageName: "tabbar_compose_button", imageName: "tabbar_compose_icon_add")
    
//    {
//       let btn = UIButton()
//        //设置背景图片
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
//        //设置图片
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
//        
//        //添加
//        self.addSubview(btn)
//        return btn
//    }()
}
